import { IMcc } from '@/api/manager/type'
export type statusType = 'vmp' | 'mix' | 'poll' | 'dcs' | 'select' | 'tvWall' | 'upMt' | 'rec' | 'doubleFlow'
export type statusMccType = 'mt' | 'subMt' | 'supMeeting' | 'subMeeting'
type statusOnlineType = Record<statusMccType, statusType[]>
export interface IStatusIcon {
	mcc: IMcc
	totalStatus: {
		vmp: boolean
		mix: boolean
		poll: boolean
		dcs: boolean
	}
	mixMode: 0 | 1 | 2
	ignoreLevel?: boolean
}
export type returnTypeStatus = {
	[key in statusMccType]: { icon: statusType[]; dropDown: statusType[] }
}
const STATUS_ONLINE: statusOnlineType = {
	mt: ['vmp', 'mix', 'poll', 'dcs', 'select', 'tvWall', 'upMt', 'rec'],
	subMt: ['vmp', 'mix', 'poll', 'dcs', 'tvWall', 'upMt', 'rec'],
	supMeeting: ['vmp', 'tvWall', 'doubleFlow'],
	subMeeting: ['vmp', 'tvWall', 'doubleFlow', 'upMt'],
}
function removeStatus(status: statusOnlineType, ...args: string[]) {
	status.mt = status.mt.filter((s) => !args.includes(s))
	status.subMt = status.subMt.filter((s) => !args.includes(s))
	status.supMeeting = status.supMeeting.filter((s) => !args.includes(s))
	status.subMeeting = status.subMeeting.filter((s) => !args.includes(s))
	return status
}
//TODO: 权限是否有用？没权限，状态应该始终为未开启状态，不应该再判断权限了
//因此此处删除关于 rec 的权限判断，后续有问题再手动加上
//此外，当终端离线，它的上传状态还是会保留，并不会自动归 0，目前不影响
//ignoreLevel 为了比如常用终端，实际上它算是只有一层了
export function getStatusIcon({ mcc, totalStatus, mixMode, ignoreLevel = false }: IStatusIcon) {
	let status = { ...STATUS_ONLINE }
	if (mcc.isMt) {
		if (!totalStatus.vmp) {
			status = removeStatus(status, 'vmp')
		}
		if (mixMode === 0) {
			status = removeStatus(status, 'mix')
		}
		if (!totalStatus.poll) {
			status = removeStatus(status, 'poll')
		}
		if (!totalStatus.dcs) {
			status = removeStatus(status, 'dcs')
		}
		if (!mcc.status.isSelect) {
			status = removeStatus(status, 'select')
		}
		if (!mcc.status.isTvWall) {
			status = removeStatus(status, 'tvWall')
		}
		if (!mcc.status.isUpMt) {
			status = removeStatus(status, 'upMt')
		}
		if (!mcc.status.isRec) {
			status = removeStatus(status, 'rec')
		}
		/**
		 * 假如一个状态都没有，那不显示任何状态
		 */
		//本级终端节点
		if (mcc.confLevel === 2 || ignoreLevel) {
			if (
				!mcc.status.isSelect &&
				!mcc.status.isTvWall &&
				!mcc.status.isUpMt &&
				!mcc.status.isRec &&
				!mcc.status.isVmp &&
				!mcc.status.isMix &&
				!mcc.status.isPoll &&
				!mcc.status.isDcs
			) {
				status.mt = []
			}
		}
		//下级终端节点
		else {
			if (
				!mcc.status.isTvWall &&
				!mcc.status.isUpMt &&
				!mcc.status.isRec &&
				!mcc.status.isVmp &&
				!mcc.status.isMix &&
				!mcc.status.isPoll &&
				!mcc.status.isDcs
			) {
				status.subMt = []
			}
		}
	} else {
		if (!totalStatus.vmp) {
			status = removeStatus(status, 'vmp')
		}
		if (!mcc.status.isTvWall) {
			status = removeStatus(status, 'tvWall')
		}
		if (!mcc.status.isDoubleFlow) {
			status = removeStatus(status, 'doubleFlow')
		}
		if (!mcc.status.isUpMt) {
			status = removeStatus(status, 'upMt')
		}
	}
	return status
}
export function statusSplitDropDown(status: statusOnlineType): returnTypeStatus {
	let result = {} as returnTypeStatus
	for (const [key, value] of Object.entries(status)) {
		result[key as statusMccType] = { icon: value.slice(0, 4), dropDown: value.slice(4) }
	}
	return result
}
